package com.yhy;

import io.netty.channel.*;

public class MyChannelOutboundHandler2 extends ChannelOutboundHandlerAdapter {

    /**
     *
     * @param ctx
     * @param msg
     * @param promise
     * @throws Exception
     */
//    @Override
//    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
//        System.out.println("MyChannelOutboundHandler2"+ " write()");
//        // 执行下一个OutboundHandler
//        super.write(ctx, msg, promise);
//    }

    /**
     * 多个出站 write() 通过形参 promise 进行通信。在最后一个完成之后要手动调用 promise.setSuccess()
     * @param ctx
     * @param msg
     * @param promise
     * @throws Exception
     */
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        System.out.println("MyChannelOutboundHandler2"+ " write()");
        //等待整个链执行完的通知
        promise.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                System.out.println("promise: MyChannelOutboundHandler2 收到 promise 最终在最终执行完毕的通知");
            }
        });

        // 执行下一个OutboundHandler
        super.write(ctx, msg, promise);
    }
}
